var $sections = $('.container'); //选取容器
var inScreen = 100; //进入屏幕100px后高亮
var outScreen = 200; //距离滚出屏幕还有200px时取消高亮
console.log('222222',$sections)
function scrollactive(container) {
  container.each(function(idx, $sec) {
    var $sec = $($sec);
    var wHeight = $(window).height(); //获取窗口的高度
    var sHeight = $sec.height(); //获取元素的高度
    var wScroll = $(window).scrollTop(); //获取窗口距离网页顶部的高度
    var sScroll = $sec.offset().top; //获取元素距离顶部的高度
    wTop = wScroll + wHeight - inScreen; //窗口底部距离网页顶端的高度
    var sTop = sScroll + sHeight - outScreen; //元素底部距离网页顶端的高度
    // console.log('index====>',idx)
    //给当前所有在浏览器窗口内显示的菜单及其容器添加高亮样式
    if (sScroll < wScroll + 60) { //判断元素顶部、低部是否在当前窗口中
        // $sec.addClass('active'); //给容器添加高亮样式
        var idn = $sec.attr('id'); //获取容器的id值
        $('.' + idn).addClass('active'); //给对应的菜单添加高亮样式
        $('.' + idn).siblings('li').removeClass('active');
    } else {
        $sec.removeClass('active');
        var idr = $sec.attr('id');
        $('.menu-list li').parent('li').removeClass('active');
    };
  });
};
scrollactive($sections);

//锚点平滑滚动
function backMenu(){
  $('body').on('click','.menu-list li',function(){
    var name =  $(this).attr('data-id');
    var idName = '#' + name;
    var className = '.' + name
    setTimeout(()=>{
      console.log('点击了')
      $('.menu-list li').removeClass('active');
      $(className).addClass('active');
    },500)
    $('html, body').animate({
        scrollTop: ($(idName).offset().top - 30) + 'px'
    }, {
        duration: 400,
        easing: 'swing'
    });
    return false;
  })
}


function menuScroll(container){
  $(window).scroll(function() {
      var navTop = $('.ny-header .bg').offset().top;
      var winTop = document.documentElement.scrollTop;
      if (winTop < navTop) {
          $('.ny-header').removeClass('fixed')
      } else {
          $('.ny-header').addClass('fixed')
      }
      scrollactive(container);
  });
  //返回
  backMenu()
}